var api = require('../../../../api.js');
var utils = require('../../../../utils.js');
var WxParse = require('../../../../wxParse/wxParse.js');
var app = getApp();

// pages/integral/shopdetail/shopdetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        choose: false,

    },

    // clickchoose: function (type) {
    //     var page = this;
    //     if (!page.data.show_attr_picker) {
    //         page.setData({
    //             show_attr_picker: true,
    //         });
    //         return true;
    //     }
    //     if (page.data.miaosha_data && page.data.miaosha_data.rest_num > 0 && page.data.form.number > page.data.miaosha_data.rest_num) {
    //         wx.showToast({
    //             title: "商品库存不足，请选择其它规格或数量",
    //             image: "/images/icon-warning.png",
    //         });
    //         return true;
    //     }

    //     if (page.data.form.number > page.data.goods.num) {
    //         wx.showToast({
    //             title: "商品库存不足，请选择其它规格或数量",
    //             image: "/images/icon-warning.png",
    //         });
    //         return true;
    //     }
    //     var attr_group_list = page.data.attr_group_list;
    //     var checked_attr_list = [];
    //     for (var i in attr_group_list) {
    //         var attr = false;
    //         for (var j in attr_group_list[i].attr_list) {
    //             if (attr_group_list[i].attr_list[j].checked) {
    //                 attr = {
    //                     attr_id: attr_group_list[i].attr_list[j].attr_id,
    //                     attr_name: attr_group_list[i].attr_list[j].attr_name,
    //                 };
    //                 break;
    //             }
    //         }
    //         if (!attr) {
    //             wx.showToast({
    //                 title: "请选择" + attr_group_list[i].attr_group_name,
    //                 image: "/images/icon-warning.png",
    //             });
    //             return true;
    //         } else {
    //             checked_attr_list.push({
    //                 attr_group_id: attr_group_list[i].attr_group_id,
    //                 attr_group_name: attr_group_list[i].attr_group_name,
    //                 attr_id: attr.attr_id,
    //                 attr_name: attr.attr_name,
    //             });
    //         }
    //     }
    //     if (type == 'ADD_CART') {//加入购物车
    //         wx.showLoading({
    //             title: "正在提交",
    //             mask: true,
    //         });
    //         app.request({
    //             url: api.cart.add_cart,
    //             method: "POST",
    //             data: {
    //                 goods_id: page.data.id,
    //                 attr: JSON.stringify(checked_attr_list),
    //                 num: page.data.form.number,
    //             },
    //             success: function (res) {
    //                 wx.showToast({
    //                     title: res.msg,
    //                     duration: 1500
    //                 });
    //                 wx.hideLoading();
    //                 page.setData({
    //                     show_attr_picker: false,
    //                 });

    //             }
    //         });
    //     }
    //     if (type == 'BUY_NOW') {//立即购买
    //         page.setData({
    //             show_attr_picker: false,
    //         });
    //         wx.redirectTo({
    //             url: "/pages/order-submit/order-submit?goods_info=" + JSON.stringify({
    //                 goods_id: page.data.id,
    //                 attr: checked_attr_list,
    //                 num: page.data.form.number,
    //             }),
    //         });
    //     }

    // },




















    // 选择规格
    clickchoose: function (e) {


        var page = this
        console.log(e.currentTarget.dataset)
        console.log("=======================")
        var attr_group_id = e.currentTarget.dataset.attr_group_id
        var attr_id = e.currentTarget.dataset.attr_id
        var index = e.currentTarget.dataset.index
        var attr_group_list = page.data._goods_info.attr_group_list

        console.log(attr_group_list[index].attr_list)

        for (var i in attr_group_list) {
            if (attr_group_list[i].attr_group_id != attr_group_id)
                continue;
            for (var j in attr_group_list[i].attr_list) {
                if (attr_group_list[i].attr_list[j].attr_id == attr_id) {
                    attr_group_list[i].attr_list[j].checked = true;
                } else {
                    attr_group_list[i].attr_list[j].checked = false;
                }
            }
        }
        page.setData({
            attr_group_list: attr_group_list,
        });

        var check_attr_list = [];
        var check_all = true;
        for (var i in attr_group_list) {
            var group_checked = false;
            for (var j in attr_group_list[i].attr_list) {
                if (attr_group_list[i].attr_list[j].checked) {
                    check_attr_list.push(attr_group_list[i].attr_list[j].attr_id);
                    group_checked = true;
                    break;
                }
            }
            if (!group_checked) {
                check_all = false;
                break;
            }
        }
        if (!check_all)
            return;
        wx.showLoading({
            title: "正在加载",
            mask: true,
        });

        page.setData({
            attr_list: JSON.stringify(check_attr_list)
        })


        app.request({
            url: api.integralmall.goods_info,
            data: {
                goods_id: page.data.goods_id,
                attr_list: JSON.stringify(check_attr_list),
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
            }
        });
    },
    // 防止遮罩层穿透
    myCatchTouch: function () {
        console.log('stop user scroll it!');
        return;
    },

    // 兑换
    exchange: function () {
        var that = this

        let cat_id = that.data.cat_id;
        let id_index = that.data.id_index;
        let attr_list = that.data.attr_list
        wx.navigateTo({
            url: '/pages/integral/okorder/okorder?attr_list=' + attr_list + '&id_index=' + id_index + '&cat_id=' + cat_id,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })

    },
    //
    swiperChange: function (e) {
        this.setData({
            swiperCurrent: e.detail.current //获取当前轮播图片的下标
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        console.log("============================")
        var that = this
        // var cat_id = options.cat_id
        // var id_index = options.id_index
        var id = options.id
        // that.setData({
        //     id_index: id_index,
        //     cat_id: cat_id
        // })
        app.request({
            url: api.integralmall.goods_info,
            data: {
                id: id
            },
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
                console.log(res.data)
                console.log("-==------------------------------------")
                that.setData({
                    _goods_info: res.data,
                    goods_id: res.data.goods.id
                })
                // 富文本 渲染
                var article = that.data._goods_info.goods.detail
                WxParse.wxParse('article', 'html', article, that, 5)
            },
            fail: function (res) { },
            complete: function (res) { },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})